package com.ddpt.platform.common.base;

import com.ddpt.platform.common.exception.CommonException;
import com.ddpt.platform.common.exception.ErrorEnum;
import lombok.Data;

@Data
public class PageRequest extends BaseEntity {

    private Long current = 1L;

    private Long size = 30L;

    public PageRequest() {}

    public PageRequest(Long current, Long size) {
        if(null == current || current <= 0) {
            throw new CommonException(ErrorEnum.PAGE_ERROR);
        }
        if(null == size || size <= 0) {
            throw new CommonException(ErrorEnum.PAGE_SIZE_ERROR);
        }
        this.current = current;
        this.size = size;
    }

    public void setCurrent(Long current) {
        if(null == current || current <= 0) {
            throw new CommonException(ErrorEnum.PAGE_ERROR);
        }
        this.current = current;
    }

    public void setSize(Long size) {
        if(null == size || size <= 0) {
            throw new CommonException(ErrorEnum.PAGE_SIZE_ERROR);
        }
        this.size = size;
    }

    public Long getOffset() {
        if(null == current || current <= 0) {
            throw new CommonException(ErrorEnum.PAGE_ERROR);
        }
        return Long.valueOf((current - 1) * size);
    }
}
